/**
 * @file ai_glass_callback.h
 * @brief AI Glass回调接口框架头文件
 * @author [Your Name]
 * @date 2025-09-28
 * @version 1.0
 * @copyright Copyright (c) 2025
 */

#ifndef _AI_GLASS_CALLBACK_H__
#define _AI_GLASS_CALLBACK_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include "ai_glass_common.h"

/* ==================== 错误码定义 ==================== */

/**
 * @brief 系统错误码定义
 */
typedef enum {
    // 成功状态
    AI_GLASS_ERR_SUCCESS            = 0x0000,      // 操作成功
    
    // 系统级错误 (0x1000-0x1FFF)
    AI_GLASS_ERR_SYSTEM_BASE        = 0x1000,
    AI_GLASS_ERR_INIT_FAILED        = 0x1001,      // 初始化失败
    AI_GLASS_ERR_MEMORY_ALLOC       = 0x1002,      // 内存分配失败
    AI_GLASS_ERR_INVALID_PARAM      = 0x1003,      // 无效参数
    AI_GLASS_ERR_NULL_POINTER       = 0x1004,      // 空指针错误
    AI_GLASS_ERR_TIMEOUT            = 0x1005,      // 操作超时
    AI_GLASS_ERR_BUSY               = 0x1006,      // 系统忙碌
    AI_GLASS_ERR_NOT_INITIALIZED    = 0x1007,      // 未初始化
    AI_GLASS_ERR_ALREADY_RUNNING    = 0x1008,      // 已在运行
    AI_GLASS_ERR_NOT_RUNNING        = 0x1009,      // 未在运行
    
    // IPI通信错误 (0x2000-0x2FFF)
    AI_GLASS_ERR_IPI_BASE           = 0x2000,
    AI_GLASS_ERR_IPI_DEVICE_OPEN    = 0x2001,      // IPI设备打开失败
    AI_GLASS_ERR_IPI_IOCTL_FAILED   = 0x2002,      // ioctl调用失败
    AI_GLASS_ERR_IPI_CMD_INVALID    = 0x2003,      // 无效IPI命令
    AI_GLASS_ERR_IPI_RESPONSE_TIMEOUT = 0x2004,    // IPI响应超时
    AI_GLASS_ERR_IPI_MMAP_FAILED    = 0x2005,      // 共享内存映射失败
    AI_GLASS_ERR_IPI_DATA_TOO_LARGE = 0x2006,      // 数据过大
    
    // 文件操作错误 (0x3000-0x3FFF)
    AI_GLASS_ERR_FILE_BASE          = 0x3000,
    AI_GLASS_ERR_FILE_OPEN_FAILED   = 0x3001,      // 文件打开失败
    AI_GLASS_ERR_FILE_READ_FAILED   = 0x3002,      // 文件读取失败
    AI_GLASS_ERR_FILE_WRITE_FAILED  = 0x3003,      // 文件写入失败
    AI_GLASS_ERR_FILE_SEEK_FAILED   = 0x3004,      // 文件定位失败
    AI_GLASS_ERR_FILE_CLOSE_FAILED  = 0x3005,      // 文件关闭失败
    AI_GLASS_ERR_FILE_NOT_EXIST     = 0x3006,      // 文件不存在
    AI_GLASS_ERR_FILE_PERMISSION    = 0x3007,      // 文件权限错误
    AI_GLASS_ERR_DISK_FULL          = 0x3008,      // 磁盘空间不足
    
    // 相机/拍照错误 (0x4000-0x4FFF)
    AI_GLASS_ERR_CAMERA_BASE        = 0x4000,
    AI_GLASS_ERR_CAMERA_INIT_FAILED = 0x4001,      // 相机初始化失败
    AI_GLASS_ERR_CAMERA_NOT_READY   = 0x4002,      // 相机未就绪
    AI_GLASS_ERR_CAPTURE_FAILED     = 0x4003,      // 拍照失败
    AI_GLASS_ERR_STORAGE_FULL       = 0x4004,      // 存储空间不足
    AI_GLASS_ERR_FORMAT_NOT_SUPPORT = 0x4005,      // 格式不支持
    
    // 录像错误 (0x5000-0x5FFF)
    AI_GLASS_ERR_RECORD_BASE        = 0x5000,
    AI_GLASS_ERR_RECORD_INIT_FAILED = 0x5001,      // 录像初始化失败
    AI_GLASS_ERR_RECORD_START_FAILED = 0x5002,     // 录像开始失败
    AI_GLASS_ERR_RECORD_STOP_FAILED = 0x5003,      // 录像停止失败
    AI_GLASS_ERR_RECORD_TIME_TOO_SHORT = 0x5004,   // 录像时间过短
    AI_GLASS_ERR_ENCODER_ERROR      = 0x5005,      // 编码器错误
    
    // 音频错误 (0x6000-0x6FFF)
    AI_GLASS_ERR_AUDIO_BASE         = 0x6000,
    AI_GLASS_ERR_AUDIO_INIT_FAILED  = 0x6001,      // 音频初始化失败
    AI_GLASS_ERR_AUDIO_DEVICE_BUSY  = 0x6002,      // 音频设备忙碌
    AI_GLASS_ERR_AUDIO_FORMAT_ERROR = 0x6003,      // 音频格式错误
    AI_GLASS_ERR_AUDIO_RECORD_FAILED = 0x6004,     // 音频录制失败
    
    // 网络/RTSP错误 (0x7000-0x7FFF)
    AI_GLASS_ERR_NETWORK_BASE       = 0x7000,
    AI_GLASS_ERR_RTSP_INIT_FAILED   = 0x7001,      // RTSP初始化失败
    AI_GLASS_ERR_RTSP_START_FAILED  = 0x7002,      // RTSP启动失败
    AI_GLASS_ERR_RTSP_STOP_FAILED   = 0x7003,      // RTSP停止失败
    AI_GLASS_ERR_NETWORK_CONNECT    = 0x7004,      // 网络连接失败
    AI_GLASS_ERR_WIFI_CONNECT       = 0x7005,      // WiFi连接失败
    AI_GLASS_ERR_FTP_ERROR          = 0x7006,      // FTP错误
    
    // 其他错误
    AI_GLASS_ERR_UNKNOWN            = 0xFFFF       // 未知错误
} ai_glass_error_code_t;

/* ==================== 状态定义 ==================== */

/**
 * @brief 任务状态枚举
 */
typedef enum {
    AI_GLASS_STATE_IDLE             = 0,           // 空闲状态
    AI_GLASS_STATE_INITIALIZING     = 1,           // 初始化中
    AI_GLASS_STATE_READY            = 2,           // 就绪状态
    AI_GLASS_STATE_RUNNING          = 3,           // 运行中
    AI_GLASS_STATE_PAUSED           = 4,           // 暂停状态
    AI_GLASS_STATE_STOPPING         = 5,           // 停止中
    AI_GLASS_STATE_ERROR            = 6,           // 错误状态
    AI_GLASS_STATE_COMPLETED        = 7            // 完成状态
} ai_glass_state_t;

/**
 * @brief 模块类型枚举
 */
typedef enum {
    AI_GLASS_MODULE_SYSTEM          = 0,           // 系统模块
    AI_GLASS_MODULE_IPI             = 1,           // IPI通信模块
    AI_GLASS_MODULE_CAMERA          = 2,           // 相机模块
    AI_GLASS_MODULE_RECORD          = 3,           // 录像模块
    AI_GLASS_MODULE_AUDIO           = 4,           // 音频模块
    AI_GLASS_MODULE_RTSP            = 5,           // RTSP模块
    AI_GLASS_MODULE_FILE            = 6,           // 文件系统模块
    AI_GLASS_MODULE_NETWORK         = 7,           // 网络模块
    AI_GLASS_MODULE_MAX
} ai_glass_module_t;

/* ==================== 回调数据结构 ==================== */

/**
 * @brief 回调事件信息结构体
 */
typedef struct {
    uint32_t            task_id;        // 任务ID
    ai_glass_module_t   module;         // 模块类型
    uint32_t            cmd;            // 命令类型
    ai_glass_state_t    state;          // 当前状态
    ai_glass_error_code_t error_code;   // 错误码
    uint64_t            timestamp;      // 时间戳(毫秒)
    uint32_t            progress;       // 进度百分比(0-100)
    uint32_t            data_len;       // 附加数据长度
    void*               data;           // 附加数据指针
    char                message[128];   // 详细消息
} ai_glass_callback_info_t;

/**
 * @brief 回调函数类型定义
 * @param info 回调信息结构体指针
 * @param user_data 用户自定义数据指针
 */
typedef void (*ai_glass_callback_func_t)(const ai_glass_callback_info_t* info, void* user_data);

/**
 * @brief 回调注册信息结构体
 */
typedef struct ai_glass_callback_node {
    ai_glass_module_t           module;         // 关注的模块
    uint32_t                    cmd_mask;       // 关注的命令掩码(0表示关注所有)
    ai_glass_state_t            state_mask;     // 关注的状态掩码
    ai_glass_callback_func_t    callback;       // 回调函数
    void*                       user_data;      // 用户数据
    struct ai_glass_callback_node* next;        // 链表指针
} ai_glass_callback_node_t;

/* ==================== 接口函数声明 ==================== */

/**
 * @brief 初始化回调框架
 * @return 0成功，其他值失败
 */
int ai_glass_callback_init(void);

/**
 * @brief 销毁回调框架
 * @return 0成功，其他值失败
 */
int ai_glass_callback_destroy(void);

/**
 * @brief 注册回调函数
 * @param module 模块类型
 * @param cmd_mask 命令掩码(0表示关注所有命令)
 * @param state_mask 状态掩码(0表示关注所有状态)
 * @param callback 回调函数指针
 * @param user_data 用户数据指针
 * @return 回调句柄，NULL表示失败
 */
ai_glass_callback_node_t* ai_glass_callback_register(
    ai_glass_module_t module,
    uint32_t cmd_mask,
    ai_glass_state_t state_mask,
    ai_glass_callback_func_t callback,
    void* user_data
);

/**
 * @brief 注销回调函数
 * @param handle 回调句柄
 * @return 0成功，其他值失败
 */
int ai_glass_callback_unregister(ai_glass_callback_node_t* handle);

/**
 * @brief 发送回调通知
 * @param info 回调信息结构体指针
 * @return 0成功，其他值失败
 */
int ai_glass_callback_notify(const ai_glass_callback_info_t* info);

/**
 * @brief 创建回调信息结构体
 * @param task_id 任务ID
 * @param module 模块类型
 * @param cmd 命令类型
 * @param state 状态
 * @param error_code 错误码
 * @param progress 进度(0-100)
 * @param message 消息字符串
 * @return 回调信息结构体指针，NULL表示失败
 */
ai_glass_callback_info_t* ai_glass_callback_create_info(
    uint32_t task_id,
    ai_glass_module_t module,
    uint32_t cmd,
    ai_glass_state_t state,
    ai_glass_error_code_t error_code,
    uint32_t progress,
    const char* message
);

/**
 * @brief 销毁回调信息结构体
 * @param info 回调信息结构体指针
 */
void ai_glass_callback_destroy_info(ai_glass_callback_info_t* info);

/**
 * @brief 获取错误码描述字符串
 * @param error_code 错误码
 * @return 错误描述字符串
 */
const char* ai_glass_get_error_string(ai_glass_error_code_t error_code);

/**
 * @brief 获取状态描述字符串
 * @param state 状态值
 * @return 状态描述字符串
 */
const char* ai_glass_get_state_string(ai_glass_state_t state);

/**
 * @brief 获取模块名称字符串
 * @param module 模块类型
 * @return 模块名称字符串
 */
const char* ai_glass_get_module_string(ai_glass_module_t module);

/* ==================== 便捷宏定义 ==================== */

/**
 * @brief 快速发送成功通知
 */
#define AI_GLASS_NOTIFY_SUCCESS(task_id, module, cmd, progress, msg) \
    do { \
        ai_glass_callback_info_t* info = ai_glass_callback_create_info( \
            task_id, module, cmd, AI_GLASS_STATE_COMPLETED, \
            AI_GLASS_ERR_SUCCESS, progress, msg); \
        if (info) { \
            ai_glass_callback_notify(info); \
            ai_glass_callback_destroy_info(info); \
        } \
    } while(0)

/**
 * @brief 快速发送错误通知
 */
#define AI_GLASS_NOTIFY_ERROR(task_id, module, cmd, error_code, msg) \
    do { \
        ai_glass_callback_info_t* info = ai_glass_callback_create_info( \
            task_id, module, cmd, AI_GLASS_STATE_ERROR, \
            error_code, 0, msg); \
        if (info) { \
            ai_glass_callback_notify(info); \
            ai_glass_callback_destroy_info(info); \
        } \
    } while(0)

/**
 * @brief 快速发送进度通知
 */
#define AI_GLASS_NOTIFY_PROGRESS(task_id, module, cmd, progress, msg) \
    do { \
        ai_glass_callback_info_t* info = ai_glass_callback_create_info( \
            task_id, module, cmd, AI_GLASS_STATE_RUNNING, \
            AI_GLASS_ERR_SUCCESS, progress, msg); \
        if (info) { \
            ai_glass_callback_notify(info); \
            ai_glass_callback_destroy_info(info); \
        } \
    } while(0)

/**
 * @brief 快速发送状态变化通知
 */
#define AI_GLASS_NOTIFY_STATE(task_id, module, cmd, state, msg) \
    do { \
        ai_glass_callback_info_t* info = ai_glass_callback_create_info( \
            task_id, module, cmd, state, \
            AI_GLASS_ERR_SUCCESS, 0, msg); \
        if (info) { \
            ai_glass_callback_notify(info); \
            ai_glass_callback_destroy_info(info); \
        } \
    } while(0)

#ifdef __cplusplus
}
#endif

#endif /* _AI_GLASS_CALLBACK_H__ */